package com.taru.controller;


import com.taru.model.Admin;
import com.taru.service.AdminService;
import com.taru.utils.Constant;
import com.taru.utils.JsonResult;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@CrossOrigin
public class AdminController {
    private static Logger logger = LoggerFactory.getLogger(AdminController.class);


    @Autowired
    AdminService adminService;


    @RequestMapping("/login/admin")
    @ResponseBody
    public Object queryFruitsById(@Param("adminName")String adminName, @Param("adminPassword")String adminPassword){
        JsonResult result;
        try {
            Admin admin= adminService.queryAdmin(adminName,adminPassword);

            result = new JsonResult("查询成功", Constant.STATUC_SUCCESS, admin);
            logger.info("查询结果", admin.toString());
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("查询异常", Constant.STATUC_FAILURE);
        }
        return result;
    }



    @RequestMapping("/update/admin/id")
    @ResponseBody
    public Object updateAdminById(Admin admin){
        JsonResult result = null;
        try {
            int i = adminService.updateAdminById(admin);
            if(i == 0){
                logger.info("修改", admin.toString());
                result = new JsonResult("修改失败", Constant.STATUC_FAILURE);
            }else if(i == 1){
                logger.info("修改", admin.toString());
                result = new JsonResult("修改成功", Constant.STATUC_SUCCESS);
            }
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("修改异常", Constant.STATUC_FAILURE);
        }
        return result;
    }
}
